﻿using eslib.tools.config;

namespace eslib.NetUpdator
{
    public class UpdatorConfig
    {
        /// <summary>
        /// 版本名称
        /// </summary>
        public string VersionName { get; set; }

        /// <summary>
        /// 版本号，数字越大越新
        /// </summary>
        public int Version { get; set; }

        /// <summary>
        /// 下载的zip文件
        /// </summary>
        public string ZipFileUrl { get; set; }

        /// <summary>
        /// 解压目录
        /// </summary>
        public string ExtraDir { get; set; }

        /// <summary>
        /// 解压的基础目录
        /// </summary>
        public static string ExtraBaseDir { get; set; } = "";

        /// <summary>
        /// 解压目录的完整路径
        /// </summary>
        public string ExtraDirFullPath()
        {
            return Path.Combine(ExtraBaseDir, ExtraDir);
        }





        public UpdatorConfig()
        {
        }

        public UpdatorConfig(string versionName, int version, string zipfileUrl, string extraDir)
        {
            VersionName = versionName;
            Version = version;
            ZipFileUrl = zipfileUrl;
            ExtraDir = extraDir;
        }


        /// <summary>
        /// 本地配置文件
        /// </summary>
        public static UpdatorConfig Local
        {
            get
            {
                if (_instance == null)
                {
                    //本地配置不存在则创建默认配置文件
                    if (!File.Exists(LocalFullPath))
                    {
                        var def = New();
                        Update(def);
                    }

                    _instance = GenericJsonConfig.Load<UpdatorConfig>(LocalFullPath);
                }
                return _instance;
            }
        }
        static UpdatorConfig _instance = null;


        /// <summary>
        /// 与target比较，需要更新返回true
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public bool NeedUpdate(UpdatorConfig target)
        {
            return target.Version > this.Version;
        }


        public static UpdatorConfig LoadFromJson(string json)
        {
            return GenericJsonConfig.LoadFromString<UpdatorConfig>(json);
        }


        /// <summary>
        /// 更新本地配置文件
        /// </summary>
        /// <param name="config"></param>
        /// <exception cref="NotImplementedException"></exception>
        public static void Update(UpdatorConfig config)
        {
            GenericJsonConfig.Save(config, LocalFullPath);
        }




        /// <summary>
        /// 本地json配置文件名
        /// </summary>
        public static string LocalConfigJsonPath = "netupdator_config.json";

        /// <summary>
        /// 本地配置文件基础目录
        /// MAUI等需要变换相对路径的可修改这个属性
        /// </summary>
        public static string LocalConfigBaseDir = "./config/";

        /// <summary>
        /// 本地完整配置文件路径
        /// </summary>
        public static string LocalFullPath
        {
            get
            {
                if (!Directory.Exists(LocalConfigBaseDir))
                {
                    Directory.CreateDirectory(LocalConfigBaseDir);
                }
                return Path.Combine(LocalConfigBaseDir, LocalConfigJsonPath);
            }
        }




        public static UpdatorConfig New()
        {
            return new UpdatorConfig("default", 0, "", "wwwroot");
        }




    }
}
